﻿using EFFC.Frame.Net.Module.Extend.EWRA.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFFC.Extends.LinqDLR2SQL;
using EFFC.Frame.Net.Unit.DB;
using EFFC.Frame.Net.Base.Data.Base;
using EFFC.Frame.Net.Base.Data;
using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Module.Extend.EWRA.Attributes.Validation;
using ICPFrameLib.Business;
using ICPFrameLib.Attributes;
using EFFC.Frame.Net.Unit.DB.Parameters;

namespace XHRPMIS.Business.v1._0
{
     public class InnerMsg : MyRestLogic
{
    [EWRARoute("get", "/innermsg/mine")]
    [EWRARouteDesc("获取当前登录者的未读站内信")]
    [EWRAOutputDesc("返回结果", @"{
code:""success-成功，failed-失败"",
msg:""提示信息"",
data:[{
uid:uid,
title:标题,
category:分类,
content:内容,
from_name:发送者,
to_name:接收者,
url:跳转链接,
add_time:新增时间
}]
}")]
    public object GetMyMsg()
    {
        SetCacheEnable(false);

        var up = DB.NewMetaUnitParameter();
        var s = from t in DB.LamdaTable(up, "EXTEND_INNERMSG", "a")
                where t.IsRead != 1 && t.ToLoginID == TokenPayLoad.ID
                orderby t.add_time descending
                select new
                {
                    uid = t.UID,
                    title = t.Title,
                    category = t.Category,
                    content = t.Content,
                    from_name = t.FromName,
                    to_name = t.ToName,
                    url = t.Url,
                    attachments = t.Attachments,
                    other_parameters = t.OtherParameters,
                    t.add_time
                };
        return new
        {
            code = "success",
            msg = "",
            data = s.GetQueryList(up).Select(d =>
            {
                d.add_time = DateTimeStd.IsDateTimeThen(d.add_time, "yyyy-MM-dd HH:mm:ss");
                return d;
            })
        };
    }

    [EWRARoute("post", "/innermsg")]
    [EWRARouteDesc("新增一条站内信")]
    [EWRAEmptyValid("to_loginid,title,content")]
    [EWRAAddInput("to_loginid", "string", "接收者的登录账号,多个用逗号分隔", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
    [EWRAAddInput("title", "string", "标题", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
    [EWRAAddInput("category", "string", "分类", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, true)]
    [EWRAAddInput("content", "string", "内容", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
    [EWRAAddInput("url", "string", "链接", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
    [EWRAAddInput("attachements", "string", "附件信息", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
    [EWRAAddInput("other_parameters", "string", "其它参数", "无默认值", EFFC.Frame.Net.Base.Constants.RestInputPosition.PostData, false)]
    [EWRAOutputDesc("返回结果", @"{
code:""success-成功，failed-失败"",
msg:""提示信息""
}")]
    public object AddMsg()
    {
        var to_loginid = ComFunc.nvl(PostDataD.to_loginid);
        var title = ComFunc.nvl(PostDataD.title);
        var category = ComFunc.nvl(PostDataD.category);
        var content = ComFunc.nvl(PostDataD.content);
        var url = ComFunc.nvl(PostDataD.url);
        var attachements = ComFunc.nvl(PostDataD.attachements);
        var other_parameters = ComFunc.nvl(PostDataD.other_parameters);

        var up = DB.NewMetaUnitParameter();
        var msg = "";
        if (DoAddMsg(up, to_loginid, title, category, content, url, attachements, other_parameters, ref msg, TokenPayLoad.ID, ComFunc.nvl(TokenPayLoad["name"]), ClientInfo.IP))
        {
            return new
            {
                code = "success",
                msg = "操作成功"
            };
        }
        else
        {
            return new
            {
                code = "failed",
                msg
            };
        }
    }
    [EWRARoute("patch", "/innermsg/{uid}/read")]
    [EWRARouteDesc("已读一条站内信")]
    [EWRAOutputDesc("返回结果", @"{
code:""success-成功，failed-失败"",
msg:""提示信息""
}")]
    public object DoRead(string uid)
    {
        var up = DB.NewMetaUnitParameter();
        var tologinlist = (from t in DB.LamdaTable(up, "EXTEND_INNERMSG", "a")
                           where t.UID == uid
                           select t).GetQueryList(up);
        if (tologinlist.Count <= 0)
        {
            return new
            {
                code = "failed",
                msg = "无该信息"
            };
        }
        dynamic tologininfo = tologinlist.First();
        DB.QuickUpdate(up, "EXTEND_INNERMSG", new
        {
            IsRead = 1,
            last_id = TokenPayLoad.ID,
            last_name = ComFunc.nvl(TokenPayLoad["name"]),
            last_ip = ClientInfo.IP,
            last_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
        }, new
        {
            UID = uid
        });
        return new
        {
            code = "success",
            msg = "操作成功"
        };
    }

    public bool DoAddMsg(UnitParameter metaup, string to_loginid, string title, string category, string content, string url, string attachements, string other_parameters, ref string msg,
        string login_id = "", string login_name = "", string login_ip = "")
    {
        var tologinlist = (from t in DB.LamdaTable(metaup, "EXTEND_LOGIN", "a")
                           where t.LoginID.within(to_loginid)
                           select t).GetQueryList(metaup);
        //if (tologinlist.Count <= 0)
        //{
        //    msg = "无该接收人员";
        //    return false;
        //}
        var tos = to_loginid.Split(",");
        foreach (var to in tos)
        {
            dynamic item = tologinlist.Where(w => ComFunc.nvl(w.GetValue("LoginID")) == to).FirstOrDefault();
            DB.QuickInsert(metaup, "EXTEND_INNERMSG", new
            {
                UID = Guid.NewGuid().ToString(),
                Title = title,
                Category = category,
                Content = content,
                FromLoginID = login_id,
                FromName = login_name,
                ToLoginID = to,
                ToName = item == null ? "" : item.LoginName,
                SendWay = "Inner",
                IsRead = 0,
                IsSend = 0,
                Url = url,
                Attachments = attachements,
                OtherParameters = other_parameters,
                add_id = login_id,
                add_name = login_name,
                add_ip = login_ip,
                add_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                last_id = login_id,
                last_name = login_name,
                last_ip = login_ip,
                last_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
            });
        }

        return true;
    }
}
}
